import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:pornhub_app/components/pay/model/gold_model.dart';
import 'package:pornhub_app/components/pay/model/recharge_type_model.dart';
import 'package:pornhub_app/components/pay/model/vip_model.dart';
import 'package:pornhub_app/components/pay/pay_utils.dart';
import 'package:pornhub_app/http/api/api.dart';
import 'package:pornhub_app/model/mine/ann_vip_model.dart';
import 'package:pornhub_app/services/user_service.dart';
import 'package:pornhub_app/utils/logger.dart';

import '../../../model/mine/points_redemption_model.dart';

class VipPageController extends GetxController
    with GetSingleTickerProviderStateMixin {
  final userService = Get.find<UserService>();
  late TabController tabController;
  int tabInitIndex = 0;
  var tabIndex = 0.obs;
  List<Tab> tabs = const [
    Tab(text: "会员中心"),
    Tab(text: "金币充值"),
  ];
  Rx<AnnVipModel> annVip = AnnVipModel().obs;
  Rx<PointsRedemptionModel> exchangeIntegral = PointsRedemptionModel().obs;
  RxList<PrizeList> prizes = <PrizeList>[].obs;

  RxList<VipModel> vipList = <VipModel>[].obs;
  RxList<GoldModel> goldList = <GoldModel>[].obs;
  Rx<VipModel> currentVipCard = VipModel().obs;
  Rx<GoldModel> currentGoldCard = GoldModel().obs;

  ///支付用的
  var mCurrentVipPayType = RechargeTypeModel.empty();
  var mCurrentGoldPayType = RechargeTypeModel.empty();

  @override
  void onInit() {
    final arguments = Get.arguments;
    if (arguments != null) {
      tabInitIndex = Get.arguments['tabInitIndex'] ?? 0;
    }
    tabController = TabController(
        length: tabs.length, vsync: this, initialIndex: tabInitIndex);
    super.onInit();
  }

  @override
  void onReady() {
    super.onReady();
    _getVipCards();
  }

  _getVipCards() async {
    try {
      Api.getVipGoldCards().then((response) {
        if (response != null) {
          vipList.assignAll(response.vipCardList ?? []);
          goldList.assignAll(response.goldVipList ?? []);
          currentVipCard.value = vipList.first;
          currentGoldCard.value = goldList.first;
        }
      });
    } catch (e) {
      logger.e(e);
    }
  }

  startPay({
    required double amount,
    required int purType,
    required int payId,
    required RechargeTypeModel currentPayType,
  }) {
    PayUtils.checkPay(
      amount: amount,
      payNumber: 1,
      purType: purType,
      source: 0,
      payId: payId,
      balance: userService.assets.bala ?? 0,
      currentPayType: currentPayType,
      onSuccess: (){
        userService.updateAll();
      },
    );
  }
}
